import os
import re
from data_processor import DataProcessor
from file_handler import FileHandler
from data_comparator import DataComparator
from datetime import datetime

#定义总的储存位置为/data/
data_folder = "data"
# 定义第一个地址及相关变量
bxznMlList_url = "https://12366.chinatax.gov.cn/bsznBLH/getbxznMlList"
bxznMlList_filename = "bxznMlList.json"
bxznMlList_update_log_file = "update_log_bxznMlList.txt"

# 处理第一个地址的数据
processor1 = DataProcessor(bxznMlList_url)
response1 = processor1.fetch_data()
parsed_data1 = processor1.parse_data(response1, ['json', 'bxznMlList'])
if parsed_data1 is not None:
    folder_name = bxznMlList_filename.split("_")[0]
    latest_file1 = DataComparator.get_latest_file(data_folder, folder_name)
    if latest_file1 is not None:
        old_data1 = DataComparator.load_data(os.path.join(data_folder, folder_name, latest_file1))
        if DataComparator.compare_data(parsed_data1, old_data1):
            print("第一个地址数据没有变化。")
        else:
            print("第一个地址数据发生了变化。")
            differences = DataComparator.get_data_diff(old_data1, parsed_data1)
            for diff in differences:
                print(f"编号: {diff[0]}, 字段: {diff[1]}, 旧值: {diff[2]}, 新值: {diff[3]}")
            FileHandler.save_data(parsed_data1, bxznMlList_filename)
            date_str = datetime.now().strftime("%Y-%m-%d")
            FileHandler.append_update_log(bxznMlList_update_log_file, date_str, differences)
    else:
        print("没有找到第一个地址之前的数据文件，无法进行比较。")
        FileHandler.save_data(parsed_data1, bxznMlList_filename)

# 定义第二个地址及相关变量
bsznList_url = "https://12366.chinatax.gov.cn/bsznBLH/getbsznList"
bsznList_filename = "bsznList.json"
bsznList_update_log_file = "update_log_bsznList.txt"

# 处理第二个地址的数据
processor2 = DataProcessor(bsznList_url)
parsed_data2 = []
for page_index in range(1, 21): # 循环调用接口获取所有数据
    response2 = processor2.fetch_data(page_index=page_index)
    if response2.status_code == 200:
        parsed_data = processor2.parse_data(response2, ['json', 'searchResultVO', 'pageContent'])
        if parsed_data:
            parsed_data2.extend(parsed_data)
            print(f"已获取第 {page_index} 页数据")
        else:
            print(f"获取第 {page_index} 页数据失败")
    else:
        print(f"请求第 {page_index} 页数据失败，状态码：", response2.status_code)
print(f"已获取所有数据，共 {len(parsed_data2)} 条")
if parsed_data2 is not None:
    folder_name = bsznList_filename.split("_")[0]
    latest_file2 = DataComparator.get_latest_file(data_folder, folder_name)
    if latest_file2 is not None:
        old_data2 = DataComparator.load_data(os.path.join(data_folder, folder_name, latest_file2))
        if DataComparator.compare_data(parsed_data2, old_data2):
            print("第二个地址数据没有变化。")
        else:
            print("第二个地址数据发生了变化。")
            differences = DataComparator.get_data_diff(old_data2, parsed_data2)
            for diff in differences:
                print(f"编号: {diff[0]}, 字段: {diff[1]}, 旧值: {diff[2]}, 新值: {diff[3]}")
            FileHandler.save_data(parsed_data2, bsznList_filename)
            date_str = datetime.now().strftime("%Y-%m-%d")
            FileHandler.append_update_log(bsznList_update_log_file, date_str, differences)
    else:
        print("没有找到第二个地址之前的数据文件，无法进行比较。")
        FileHandler.save_data(parsed_data2, bsznList_filename)


# 提取包含“BH”的列表
bh_list = [item["BH"] for item in parsed_data2]

# 定义第三个地址及文件名
OneBszn_url_prefix = "https://12366.chinatax.gov.cn/bsznBLH/getOneBszn?bh="
OneBszn_filename = "OneBszn_data.txt"
OneBszn_update_log_file = "update_log_OneBszn_data.txt"


# 处理第三个地址的数据
for znid in bh_list:
    OneBszn_url = f"{OneBszn_url_prefix}{znid}"
    processor3 = DataProcessor(OneBszn_url, request_method='GET')
    response3 = processor3.fetch_data(params={"bh": znid})
    parsed_data3 = processor3.parse_data(response3, data_path=None, is_html=True)
    div_target = parsed_data3.find('div', class_='articlecon', id='zsxq_nr')
    if parsed_data3 is not None:
        article_title = parsed_data3.find('meta', {'name': 'ArticleTitle'}).get('content')
        OneBszn_filename = f"{article_title}.txt"
        latest_file3 = DataComparator.get_latest_file(data_folder,"")

        if latest_file3 is not None:
            old_data3 = DataComparator.load_data(os.path.join(data_folder, folder_name, latest_file3))
            if DataComparator.compare_data(div_target, old_data3):
                print(f"第三个地址 (znid: {znid}) 数据没有变化。")
            else:
                print(f"第三个地址 (znid: {znid}) 数据发生了变化。")
                differences = DataComparator.get_data_diff(old_data3, div_target)
                for diff in differences:
                    print(f"编号: {diff[0]}, 字段: {diff[1]}, 旧值: {diff[2]}, 新值: {diff[3]}")
                FileHandler.save_data_without_folder_name(div_target, OneBszn_filename)
                date_str = datetime.now().strftime("%Y-%m-%d")
                FileHandler.append_update_log(OneBszn_update_log_file, date_str, differences)
        else:
            print(f"没有找到第三个地址 (znid: {znid}) 之前的数据文件，无法进行比较。")
            FileHandler.save_data_without_folder_name(div_target, OneBszn_filename)
